package com.ai.fortune_matrix;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ai.autoconfigure.vectorstore.elasticsearch.ElasticsearchVectorStoreProperties;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.chat.memory.InMemoryChatMemory;
import org.springframework.ai.embedding.EmbeddingModel;
import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.springframework.ai.vectorstore.VectorStore;
import org.springframework.ai.vectorstore.elasticsearch.ElasticsearchVectorStore;
import org.springframework.ai.vectorstore.elasticsearch.ElasticsearchVectorStoreOptions;
import org.springframework.ai.vectorstore.elasticsearch.SimilarityFunction;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;


@SpringBootApplication
public class FortuneMatrixApplication {
    private static final Logger logger = LoggerFactory.getLogger(FortuneMatrixApplication.class);
    public static void main(String[] args) {
        SpringApplication.run(FortuneMatrixApplication.class, args);
    }


    // In the real world, ingesting documents would often happen separately, on a CI
    // server or similar.
//    @Bean
//    CommandLineRunner ingestTermOfServiceToVectorStore(EmbeddingModel embeddingModel, VectorStore vectorStore,
//                                                       @Value("classpath:rag/winning_number_pool2.txt") Resource termsOfServiceDocs) {
//
//        return args -> {
//            // Ingest the document into the vector store
//            /*
//             * 1、文档读取TextReader 读取 resources/rag/terms-of-service.txt 文件内容
//             * 2、TokenTextSplitter 按token长度切分文本（避免大文本超出模型限制）
//             * 3、向量化存储 通过 VectorStore.write() 将文本向量存入内存（后续可用于RAG检索）
//             */
//            vectorStore.write(new TokenTextSplitter().transform(new TextReader(termsOfServiceDocs).read()));
//
//            // 相似性搜索检测
//            vectorStore.similaritySearch("Cancelling Bookings").forEach(doc -> {
//                logger.info("Similar Document: {}", doc.getText());
//            });
//        };
//    }


//    @Bean
//    public RestClient restClient() {
//        return RestClient.builder(new HttpHost("172.30.154.97", 59200)).build();
//    }
//
//    @Bean
//    public VectorStore vectorStore(
//            EmbeddingModel embeddingModel,
//            RestClient restClient,
//            ElasticsearchVectorStoreProperties properties
//    ) {
//        ElasticsearchVectorStoreOptions options = new ElasticsearchVectorStoreOptions();
//        options.setIndexName(properties.getIndexName());
//        options.setDimensions(properties.getDimensions());
//        options.setSimilarity(SimilarityFunction.cosine);  // 举例，与你配置一致
//
//        return ElasticsearchVectorStore.builder(restClient, embeddingModel)
//                .options(options)
//                .build();
//    }


//    /**
//     * 存储多轮对话历史（基于内存）
//     * 实现上下文感知的连续对话
//     * @return
//     */
    @Bean
    public ChatMemory chatMemory() {
        return new InMemoryChatMemory();
    }

}
